package org.dromara.tearoom.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.tearoom.domain.TerIotDevice;
import org.dromara.tearoom.domain.bo.TerIotDeviceBo;
import org.dromara.tearoom.domain.vo.TerIotDeviceVo;
import org.dromara.tearoom.service.IGeeOpenService;
import org.dromara.tearoom.service.ITerIotDeviceService;
import org.dromara.tearoom.service.IWxMaNoticeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * IoT设备
 * 前端访问路由地址为:/tearoom/iotDevice
 *
 * @author LionLi
 * @date 2025-04-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iotDevice")
public class TerIotDeviceController extends BaseController {

    private final ITerIotDeviceService terIotDeviceService;
    private final IGeeOpenService geeOpenService;
    private final IWxMaNoticeService wxMaNoticeService;

    /**
     * 查询IoT设备列表
     */
    @SaCheckPermission("tearoom:iotDevice:list")
    @GetMapping("/list")
    public TableDataInfo<TerIotDeviceVo> list(TerIotDeviceBo bo, PageQuery pageQuery) {
        return terIotDeviceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出IoT设备列表
     */
    @SaCheckPermission("tearoom:iotDevice:export")
    @Log(title = "IoT设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerIotDeviceBo bo, HttpServletResponse response) {
        List<TerIotDeviceVo> list = terIotDeviceService.queryList(bo);
        ExcelUtil.exportExcel(list, "IoT设备", TerIotDeviceVo.class, response);
    }

    /**
     * 获取IoT设备详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("tearoom:iotDevice:query")
    @GetMapping("/{id}")
    public R<TerIotDeviceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(terIotDeviceService.queryById(id));
    }

    /**
     * 新增IoT设备
     */
    @SaCheckPermission("tearoom:iotDevice:add")
    @Log(title = "IoT设备", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerIotDeviceBo bo) {
        return toAjax(terIotDeviceService.insertByBo(bo));
    }

    /**
     * 修改IoT设备
     */
    @SaCheckPermission("tearoom:iotDevice:edit")
    @Log(title = "IoT设备", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerIotDeviceBo bo) {
        return toAjax(terIotDeviceService.updateByBo(bo));
    }

    /**
     * 删除IoT设备
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tearoom:iotDevice:remove")
    @Log(title = "IoT设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terIotDeviceService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * IoT设备开关状态修改
     */
    @SaCheckPermission("tearoom:iotDevice:edit")
    @Log(title = "IoT设备开关状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody TerIotDevice terIotDevice) {
        return toAjax(terIotDeviceService.updateDeviceKeyStatus(terIotDevice.getId(), terIotDevice.getKeyStatus()));
    }

    /**
     * IoT设备状态修改
     */
    @GetMapping("/test/{deviceKey}")
    public Object changeStatus(@PathVariable String deviceKey) {
        return R.ok(geeOpenService.getAirSwitchInfo(deviceKey));
    }

    //控制器测试
    @GetMapping("/testMqtt/{type}")
    public void command(@PathVariable("type") String type){
        //geeOpenService.getDeviceInfoByMqtt("/CMwXtO/kvjmWtfaPFHH/BCDDC2555392/publish");
        wxMaNoticeService.cleanOrder(type);
    }

}
